#aika online self.inFile parser

import os
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class AikaMSH(Model3D):
    
    def __init__(self, obj3D=None, inFile=None, outFile=None):
        '''Initialize an aika self.inFile Object.'''
        
        super(AikaMSH, self).__init__("aikaMSH")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_material(self, filename):
      
        self.create_material(0)
        self.add_material_name(0, filename)
        self.add_texture_name(0, filename + ".png")

    def parse_file(self, filename):
        
        objName = filename
        self.create_object(objName)
        
        unk1, unk2, unk3, count1, vertSize, unk6, boneNum, vertNum, \
            faceNum = self.inFile.read_long(9)
        
        boneStart = self.inFile.tell()
        self.inFile.seek(0x40 * boneNum, 1)
        boneIDs = []
        for i in range(0, boneNum):
            boneId = self.inFile.read_long()
            boneIDs.append(boneId)
        self.inFile.seek(boneStart)
        
        bones = []
        for i in range(0, boneNum):
            m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, \
               m41, m42, m43, m44 = self.inFile.read_float(16)
        for i in range(0, boneNum):
            boneId = self.inFile.read_float()
        
        for i in range(0, vertNum):
            vx, vy, vz = self.inFile.read_float(3)
            if vertSize == 0x24:
                bone1, bone2, bone3, bone4 = self.inFile.read_byte(4)
                weight1 = self.inFile.read_float()
                weight1 = 1.0
                weight2, weight3, weight4 = 0, 0, 0
                self.inFile.seek(0x8, os.SEEK_CUR)
            elif vertSize == 0x28:
                weight1 = self.inFile.read_float()
                weight2 = 0
                bone1, bone2, bone3, bone4 = self.inFile.read_byte(4)
                weight3 = 0
                weight4 = 0
                self.inFile.seek(0xC, os.SEEK_CUR)
            elif vertSize == 0x2C:
                weight1 = self.inFile.read_float()
                weight2 = self.inFile.read_float()
                bone1, bone2, bone3, bone4 = self.inFile.read_byte(4)
                weight3 = 0
                weight4 = 0
                self.inFile.seek(0xC, os.SEEK_CUR)
            tu = self.inFile.read_float()
            tv = self.inFile.read_float()
            
            vx = float(vx) * -1
            
            self.create_vertex(objName, i)
            self.add_coords(objName, i, [vx, vy, vz])
            self.add_vert_uv(objName, i, [tu, tv])
        
        for i in range(0, faceNum / 3):
            f1, f2, f3 = self.inFile.read_short(3)
            
            self.create_face(objName, i)
            self.add_face_verts(objName, i, [f1, f2, f3])
            self.add_face_material(objName, i, 0)
    
def read_file(path):
    openFile = StructReader(open(path, 'rb'))
    filename, ext = os.path.splitext(os.path.basename(path))
    obj = AikaMSH(inFile=openFile)
    obj.parse_material(filename)
    obj.parse_file(filename)
    openFile.close()
    return obj

def definitions():
    
    return "\x02\x00\x00\x00", "MSH", "Aika Online MSH"